package com.example.shopping1902;

import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;

import androidx.fragment.app.Fragment;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.example.shopping1902.cart.CartActivity;
import com.example.shopping1902.me.LoginActivity;
import com.example.shopping1902.me.PersonCenterActivity;
import com.example.shopping1902.me.UserBean;
import com.example.shopping1902.utils.SaveUserUtils;
import com.squareup.picasso.Picasso;

import java.io.File;

/**
 * A simple {@link Fragment} subclass.
 */
public class MeFragment extends Fragment implements View.OnClickListener {
    Button cartBtn,historyBtn,centerBtn,aboutBtn,settingBtn;
    ImageView headIv;
    TextView nicknameTv;
    private UserBean.DataBean dataBean;
    String filepath;
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_me, container, false);
        initView(view);
        return view;
    }

    @Override
    public void onResume() {
        super.onResume();
        onResumePage();
    }

    //获取焦点时会调用的方法
    public void onResumePage() {
        super.onResume();
        dataBean = SaveUserUtils.getUserinfo();
        if (dataBean != null) {
            String nickname = dataBean.getNickname();
            nicknameTv.setText(nickname);
            filepath = getContext().getCacheDir().getAbsolutePath() + File.separator + dataBean.getPhone() + ".png";
            File headfile = new File(filepath);
            if (headfile.exists()) {
                //Picasso.with(getActivity()).load(filepath).into(headIv);
                Bitmap bitmap = BitmapFactory.decodeFile(filepath);
                headIv.setImageBitmap(bitmap);
                }
            } else {
                nicknameTv.setText("请先登录");
            }
        }

    //判断当前应用是否已经登录，已经登录，跳转到个人中心页面，未登录，跳转到登录页面
    private void startCenterPage(){
        if (dataBean==null) {
            Toast.makeText(getContext(),"还未登录，请先登录账户！",Toast.LENGTH_SHORT).show();
            Intent it = new Intent(getActivity(), LoginActivity.class);
            startActivity(it);
        }else {
            Intent it = new Intent(getActivity(), PersonCenterActivity.class);
            startActivity(it);
        }

    }
    private void initView(View v) {
        cartBtn = v.findViewById(R.id.frag_me_btn_cart);
        historyBtn = v.findViewById(R.id.frag_me_btn_history);
        centerBtn = v.findViewById(R.id.frag_me_btn_center);
        aboutBtn = v.findViewById(R.id.frag_me_btn_about);
        settingBtn = v.findViewById(R.id.frag_me_btn_setting);
        headIv = v.findViewById(R.id.frag_me_iv);
        nicknameTv = v.findViewById(R.id.frag_me_tv_name);

        cartBtn.setOnClickListener(this);
        historyBtn.setOnClickListener(this);
        centerBtn.setOnClickListener(this);
        aboutBtn.setOnClickListener(this);
        settingBtn.setOnClickListener(this);
        headIv.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.frag_me_btn_cart:
                if (dataBean == null) {
                    Toast.makeText(getActivity(),"请先登录",Toast.LENGTH_SHORT).show();

                }else {
                    Intent intent = new Intent(getActivity(), CartActivity.class);
                    startActivity(intent);
                }
                break;
            case R.id.frag_me_btn_history:
                break;
            case R.id.frag_me_btn_center:
                startCenterPage();
                break;
            case R.id.frag_me_btn_about:
                break;
            case R.id.frag_me_btn_setting:
                break;
            case R.id.frag_me_iv:
                startCenterPage();
                break;

        }

    }
}
